﻿using System;
using System.IO;

namespace Microsoft.WowAddonStudio.Lua.Loaders
{
    /// <summary>
    /// Provides the file streams from the filesystem using the TOC file's path.
    /// </summary>
    public class FileStreamProvider : StreamProvider
    {
        private readonly string tocPath, tocFolder;

        /// <summary>
        /// Initializes a new instance of the <see cref="FileStreamProvider"/> class.
        /// </summary>
        /// <param name="tocPath">The path to the table of contents (TOC) file.</param>
        public FileStreamProvider(string tocPath)
        {
            if (tocPath == null)
                throw new ArgumentNullException("tocPath");

            this.tocPath = tocPath;
            this.tocFolder = Path.GetDirectoryName(tocPath);
        }


        /// <summary>
        /// Gets the <see cref="Stream"/> to a World of Warcraft TOC file.
        /// </summary>
        /// <returns>
        /// An instance of the <see cref="Stream"/> class.
        /// </returns>
        public override Stream GetTocStream()
        {
            return File.Open(tocPath, FileMode.Open, FileAccess.Read);
        }

        /// <summary>
        /// Gets a <see cref="Stream"/> to a file.
        /// </summary>
        /// <param name="filePath">The path of the file.</param>
        /// <returns></returns>
        public override Stream GetFileStream(string filePath)
        {
            if (filePath == null)
                throw new ArgumentNullException("filePath");

            return File.Open(Path.Combine(tocFolder, filePath), FileMode.Open, FileAccess.Read);
        }
    }
}
